let names = $('.title');

for (let i = 0; i < names.length; i++) {
    let name = names[i].innerText;
    if (name.length > 100) {
        name = name.slice(0, 100) + '..';
        name = names[i].innerText = name;
    }
}
let anim;

$('.send-request').hover(function () {
    let bl = 1;
    let ball = this.childNodes[1].childNodes;
    anim = setInterval(function () {
        if (bl == 5) {
            clearInterval(anim);
        }
        $(ball[bl]).addClass('ball-anim');
        bl += 2;
    }, 300);
}, function () {
    let block = this;
    $(block.childNodes[1].childNodes).css({ 'opacity': '0' });
    clearInterval(anim);
    setTimeout(function () {
        $(block.childNodes[1].childNodes).css({ 'opacity': '1' });
        $(block.childNodes[1].childNodes).removeClass('ball-anim');
    }, 400);
});

$('.on-work').hover(function () {
    let bl = 1;
    let gear = this.childNodes[1].childNodes;
    while (bl < 6) {
        if (bl == 1 || bl == 5) {
            $(gear[bl]).addClass('small-gear-anim');
        } else if (bl == 3) {
            $(gear[bl]).addClass('big-gear-anim');
        }
        bl += 2;
    }
}, function () {
    let block = this;
    $(block.childNodes[1].childNodes).css({ 'opacity': '0' });
    clearInterval(anim);
    setTimeout(function () {
        $(block.childNodes[1].childNodes).css({ 'opacity': '1' });
        $(block.childNodes[1].childNodes).removeClass('small-gear-anim');
        $(block.childNodes[1].childNodes).removeClass('big-gear-anim');
    }, 400);
});

$('.done').hover(function () {
    let bl = 1;
    let done = this.childNodes[1].childNodes;
    while (bl < 6) {
        if (bl == 1) {
            $(done[bl]).addClass('dn1');
        } else if (bl == 3) {
            $(done[bl]).addClass('dn2');
        } else if (bl == 5) {
            $(done[bl]).addClass('dn3');
        }
        bl += 2;
    }
}, function () {
    let block = this;
    $(block.childNodes[1].childNodes).css({ 'opacity': '0' });
    clearInterval(anim);
    setTimeout(function () {
        $(block.childNodes[1].childNodes).css({ 'opacity': '1' });
        $(block.childNodes[1].childNodes).removeClass('dn1');
        $(block.childNodes[1].childNodes).removeClass('dn2');
        $(block.childNodes[1].childNodes).removeClass('dn3');
    }, 400);
});

// function filter_show() {
//     let filter = document.querySelector('.filter');

//     if ($(filter).hasClass('filter-show')) {
//         $(filter).removeClass('filter-show');
//         $('.search-button').css({'fill' : '#103b84'});
//     } else {
//         $(filter).addClass('filter-show');
//         $('.search-button').css({'fill' : '#007EFA'});
//     }
// }

// Выпадающий список

// $('.filter-input').on('click', function() {

//     let ul = $(this.parentNode).find('.list');
//     if (ul.css('display') != 'none') {
//         $('.list').css({'display':'none'});
//     } else {
//         $('.list').css({'display':'none'});
//         $(ul[0]).css({'display':'block'});
//     }
// });

// window.onclick = function(event) {

//     if (event.target.matches('.list-item')){
//         let text = event.target.innerHTML;
//         $(event.target.parentNode.parentNode).find('.filter-input')[0].value = text;
//         $('.list').css({'display':'none'});
//         $('.filter-form').submit();
//     } else if (!event.target.matches('.filter-input')) {
//         $('.list').css({'display':'none'});
//     }
//     if(has_real_time_input){
//         filter();
//     }
// }


function filter(){
    let text = $("input[name='search_names']")[0].value.toLowerCase();
    console.log(text)
    let subject = Array.from($('.filters-checkox.subject').filter((i, elem) => {return elem.checked}), (element) => element.value);
    let group = Array.from($('.filters-checkox.group').filter((i, elem) => {return elem.checked}), (element) => element.value);
    let type = Array.from($('.filters-checkox.type').filter((i, elem) => {return elem.checked}), (element) => element.value);
    let nominaitions = Array.from($('.filters-checkox.nomination').filter((i, elem) => {return elem.checked}), (element) => element.value);

    $('.project').each(function(i, element){
        if(match_property(element, group, '.student-group') && match_property(element, type, '.project-type') && 
            match_property(element, subject, '.project-subjects') && match_property(element, nominaitions, '.project-nomination')
            && contains_text(element, text)){
            element.style.display = 'flex';
        }
        else{
            element.style.display = 'none';
        }
    });
}


function contains_text(element, text){
    return element.querySelector('.title').textContent.toLowerCase().includes(text) || element.querySelector('.student').textContent.toLowerCase().includes(text);
}

function match_property(element, array, className){
    if (array.length == 0){
        return true;
    }
    elemArr = element.querySelector(className).textContent.split(',')
    return array.filter((elem) => elemArr.includes(elem)).length > 0; // пересечение массивов и проверка, что длина пересечения больше 0
}

function filters_open(btn) {
    let div = btn.parentNode;
    let content = div.querySelector('.filters-content');
    let svg = btn.querySelector('.filters-svg');
    if ($(content).hasClass('list-hidden')) {
        $(content).removeClass('list-hidden');
        $(svg).css({'transform' : 'rotate(0deg)'});
        if (content.scrollHeight > 250) {
            $(content).css({'height' : '250px'});
        } else {
            $(content).css({'height' : content.scrollHeight + 'px'});
        }
    } else {
        $(content).addClass('list-hidden');
        $(svg).css({'transform' : 'rotate(45deg)'});
    }
}

function filters_search(input) {
    let text = input.value.trim();
    let content = input.parentNode.parentNode
    let list = content.querySelectorAll('.filters-item');
    for (let i = 1; i < list.length; i++) {
        let item_text = list[i].querySelector('.filters-text').textContent;
        if (item_text.toLowerCase().match(text.toLowerCase()) || text == '') {
            $(list[i]).removeClass('hidden');
        } else {
            $(list[i]).addClass('hidden');
        }
    }
    if (content.scrollHeight > 250) {
            $(content).css({'height' : '250px'});
        } else {
            $(content).css({'height' : content.scrollHeight + 'px'});
        }
}

function filters_clear(btn) {
    let input = btn.parentNode.querySelector('.filters-input');
    input.value = '';
    filters_search(input);
}

function search_input_update(input) {
    let value = input.value;
    $('.filters-search-input').val(value);
    filter()
}